﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DepartmentOnline.Models;

namespace DepartmentOnline
{
    public partial class Plans : System.Web.UI.Page
    {
        private Pagination Pager;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Pager = new Pagination()
                {
                    BindDataAction = bindData,
                    ViewState = this.ViewState,
                    PageSize = 10
                };
                bindData();
            }
            else
            {
                Pager = new Pagination()
                {
                    BindDataAction = bindData,
                    ViewState = this.ViewState,
                    PageSize = 10
                };
                //since the pagination controls are dynamically creating its shld be re created in each page load
                Pager.BuildPagination(pnlPager);
            }
        }
        private void bindData()
        {
            using (var context = new departmentEntities())
            {
                DataTable data = context.Plans.AsEnumerable().ConvertToDT();
                Pager.BindDataWithPaging(Repeater1, data, pnlPager);
            }

        }

        protected void DeletePlan(object sender, CommandEventArgs e)
        {
            throw new NotImplementedException();
        }

        protected void EditPlan(object sender, CommandEventArgs e)
        {
            throw new NotImplementedException();
        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            // Save the uploaded file to an "Uploads" directory
            // that already exists in the file system of the 
            // currently executing ASP.NET application.  
            // Creating an "Uploads" directory isolates uploaded 
            // files in a separate directory. This helps prevent
            // users from overwriting existing application files by
            // uploading files with names like "Web.config".
            string saveDir = @"\App_Data\Plans\";

            // Get the physical file system path for the currently
            // executing application.
            string appPath = Request.PhysicalApplicationPath;

            // Before attempting to save the file, verify
            // that the FileUpload control contains a file.
            var fileUpload = PlanUp;
            var status = UploadStatusLabel;
            //var fileUpload = Repeater1.FindControlRecursive("PlanUp") as FileUpload;
            //var status = Repeater1.FindControlRecursive("UploadStatusLabel") as Label;
            if (fileUpload != null && fileUpload.HasFile)
            {
                //string savePath = appPath + saveDir +
                //    Server.HtmlEncode(PhotoUp.FileName);
                string relativePath = saveDir +
                                  Server.HtmlEncode(fileUpload.FileName);
                string savePath = appPath + relativePath;

                // Call the SaveAs method to save the 
                // uploaded file to the specified path.
                // This example does not perform all
                // the necessary error checking.               
                // If a file with the same name
                // already exists in the specified path,  
                // the uploaded file overwrites it.
                fileUpload.SaveAs(savePath);

                // Notify the user that the file was uploaded successfully.
                if (status != null) status.Text = "Файл успешно загружен";

                using(var context = new departmentEntities())
                {
                    var newEntity = new Plan()
                                        {
                                            Date = DateTime.Now,
                                            File = relativePath
                                        };
                    context.Plans.Add(newEntity);
                    context.SaveChanges();
                }
                bindData();
            }
            else
            {
                // Notify the user that a file was not uploaded.
                if (status != null) status.Text = "Укажите файл для загрузки";
            }
        }
    }
}